﻿package com.msjt.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.msjt.base.BaseAction;
import com.msjt.entity.Industry;
import com.msjt.entity.News;
import com.msjt.entity.Picture;
import com.msjt.entity.Question;
import com.msjt.entity.Video;
import com.msjt.service.IIndustryService;
import com.msjt.service.INewsService;
import com.msjt.service.IPictureService;
import com.msjt.service.IQuestionService;
import com.msjt.service.IVideoService;

/**
 * name:InitAction function:页面初始化信息Action author: kw time:2013.8
 */
@SuppressWarnings("serial")
public class InitAction extends BaseAction {
	@Resource
	private INewsService newsService;
	// 主页业务的接口
	@Resource
	private IIndustryService industryService;
	//咨询模块
	@Resource
	private IQuestionService questionService;
	//图片模块
	@Resource
	
	private IPictureService pictureService;
	//视频
	@Resource
	private IVideoService videoService;
	public void setIndustryService(IIndustryService industryService) {
		this.industryService = industryService;
	}
	public void setNewsService(INewsService newsService) {
		this.newsService = newsService;
	}
	public void setQuestionService(IQuestionService questionService) {
		this.questionService = questionService;
	}
	public void setPictureService(IPictureService pictureService) {
		this.pictureService = pictureService;
	}
	public void setVideoService(IVideoService videoService) {
		this.videoService = videoService;
	}
	// 页面初始化加载的信息
	public String initPage() throws Exception {
		// 新闻列表和置顶新闻
		List<News> newsFive = this.newsService.getNewsTopFive();
		List<News> newsSix = this.newsService.getNewsTopSix();
		//主营行业信息
		List<Industry> industryList=this.industryService.getIndustryIndex();
		//问题模块
		List<Question> questionList=this.questionService.getQuestionIndex();
		//主页面加载图片地址，5-9张，按上传时间排序
		List<Picture> pictureList=this.pictureService.getPictureIndex();
		//主页所要显示的视频
		Video video=this.videoService.getVideoIndex();
		
		// 放入请求中
		this.getRequest().put("questionList", questionList);
		this.getRequest().put("newsFive", newsFive);
		this.getRequest().put("newsSix", newsSix);
		this.getRequest().put("industryList", industryList);
		this.getRequest().put("pictureList", pictureList);
		this.getRequest().put("video", video);
		return "index";
	}
	
	//网站访问统计
	@SuppressWarnings("unchecked")
	public void toMyWebCount() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		String ip=request.getRemoteAddr().toString();
		Map<String, Integer> ipMap=null;
		//ipMap=(Map<String, Integer>) (ipMap.containsKey(ip)?new HashedMap():this.getApplication().get("ipMap"));
		Map<String , Integer> map=(Map<String, Integer>) this.getApplication().get("ipMap");
		if(map==null){
			ipMap=new HashMap<String, Integer>();
			ipMap.put(ip, 1);
		}else{
			ipMap=map;
			if(ipMap.containsKey(ip)){
				ipMap.put(ip, ipMap.get(ip)+1);
			}
		}
	}
}
